package org.csu.demopetstore.Controller;

import org.csu.demopetstore.Service.CartService;
import org.csu.demopetstore.Service.CategortService;
import org.csu.demopetstore.entity.Account;
import org.csu.demopetstore.entity.Cart;
import org.csu.demopetstore.entity.Item;
import org.csu.demopetstore.entity.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.List;

@Controller
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private CartService cartService;
    @Autowired
    private CategortService categortService;

    @RequestMapping("/AddItemtoCart")
    public String addItemtoCart(@RequestParam("workingItemId")String workingItemId, Model model, HttpSession session){
        Account account = (Account)session.getAttribute("account");
        if (account != null) {
            model.addAttribute(account);
            String username = account.getUsername();
            List<Cart> cartList =  cartService.getCartList(username);
            if (cartList == null){
                Item item = categortService.getItem(workingItemId);
                String productId = item.getProductId();
                Product product = categortService.getProduct(productId);
                Cart cart = new Cart();
                cart.setUsername(username);
                cart.setItemid(item.getItemId());
                cart.setProductid(item.getProductId());
                cart.setDescription(product.getDescn());
                cart.setQuantity(1);
                Boolean isInStock = cartService.getInstock(item.getItemId());
                cart.setStock(isInStock);
                cart.setListprice(item.getListPrice());
                try {
                BigDecimal total = (cart.getListprice()).multiply(BigDecimal.valueOf(cart.getQuantity()));
                cart.setTotal(total);
                cartService.insertCart(cart);
                }catch (Exception e){
                    e.printStackTrace();
                }
                List<Cart> cartList1 =  cartService.getCartList(username);
                model.addAttribute(cartList1);
                session.setAttribute("cartList",cartList1);

                BigDecimal subTotal = cartService.calculation(account.getUsername());
                model.addAttribute("subTotal",subTotal);
                session.setAttribute("subTotal",subTotal);

                return "cart/Cart";
            }else {
                Cart cart = cartService.getCartByUsernameAndItemid(username,workingItemId);
                if (cart != null){
                    int quantity = cart.getQuantity()+1;
                    cart.setQuantity(quantity);
                    try {
                    BigDecimal total = (cart.getListprice()).multiply(BigDecimal.valueOf(cart.getQuantity()));
                    cart.setTotal(total);
                    cartService.updateCart(cart.getUsername(),cart.getItemid(),cart);
                    }catch (Exception e){
                        e.printStackTrace();
                    }

                    List<Cart> cartList1 =  cartService.getCartList(username);
                    model.addAttribute(cartList1);
                    session.setAttribute("cartList",cartList1);

                    BigDecimal subTotal = cartService.calculation(account.getUsername());
                    model.addAttribute("subTotal",subTotal);
                    session.setAttribute("subTotal",subTotal);

                    return "cart/Cart";
                }else{
                    Item item = categortService.getItem(workingItemId);
                    String productId = item.getProductId();
                    Product product = categortService.getProduct(productId);
                    Cart newCart = new Cart();
                    newCart.setUsername(username);
                    newCart.setItemid(item.getItemId());
                    newCart.setProductid(item.getProductId());
                    newCart.setDescription(product.getDescn());
                    newCart.setQuantity(1);
                    Boolean isInStock = cartService.getInstock(item.getItemId());
                    newCart.setStock(isInStock);
                    newCart.setListprice(item.getListPrice());
                    try {
                        BigDecimal total = newCart.getListprice().multiply(BigDecimal.valueOf(newCart.getQuantity()));
                        newCart.setTotal(total);
                        cartService.insertCart(newCart);
                    }catch (Exception e){
                        e.printStackTrace();
                    }

                    List<Cart> cartList1 =  cartService.getCartList(username);
                    model.addAttribute(cartList1);
                    session.setAttribute("cartList",cartList1);

                    BigDecimal subTotal = cartService.calculation(account.getUsername());
                    model.addAttribute("subTotal",subTotal);
                    session.setAttribute("subTotal",subTotal);

                    return "cart/Cart";
                }
            }
        }else {
            return "account/SignonForm";
        }
    }

    @RequestMapping("/deleteCartItem")
    public String deleteCartItem(@RequestParam("itemid") String itemid,HttpSession session,Model model){
        Account account = (Account)session.getAttribute("account");
        model.addAttribute(account);

        cartService.deleteCartByUsernameAndItemid(account.getUsername(),itemid);
        List<Cart> cartList1 =  cartService.getCartList(account.getUsername());
        if (cartList1.size() != 0){
            model.addAttribute(cartList1);
            BigDecimal subTotal = cartService.calculation(account.getUsername());
            model.addAttribute("subTotal",subTotal);
            session.setAttribute("subTotal",subTotal);
            return "cart/Cart";
        }else {
            int subTotal = 0;
            model.addAttribute("subTotal",subTotal);
            session.setAttribute("subTotal",subTotal);
            return "cart/Cart";
        }
    }

    @RequestMapping("/updateCart")
    @ResponseBody
    public String updateCart(@RequestParam("quantity") int quantity,@RequestParam("total")int total,@RequestParam("itemid")String itemid,HttpSession session){
        Account account = (Account)session.getAttribute("account");
        Cart cart = cartService.getCartByUsernameAndItemid(account.getUsername(),itemid);
        cart.setTotal(BigDecimal.valueOf(total));
        cart.setQuantity(quantity);
        cartService.updateCart(account.getUsername(),itemid,cart);
        List<Cart> cartList1 =  cartService.getCartList(account.getUsername());
        if (cartList1.size() != 0){
            BigDecimal subTotal = cartService.calculation(account.getUsername());
            System.out.println(subTotal);
            session.setAttribute("subTotal",subTotal);
            return null;
        }else {
            int subTotal = 0;
            session.setAttribute("subTotal",subTotal);
            return null;
        }
    }

    @RequestMapping("/viewCart")
    public String viewCart(HttpSession session,Model model){
        Account account = (Account)session.getAttribute("account");
        if (account != null){
            model.addAttribute(account);
            List<Cart> cartList1 =  cartService.getCartList(account.getUsername());
            if (cartList1.size() != 0){
                model.addAttribute(cartList1);
                BigDecimal subTotal = cartService.calculation(account.getUsername());
                model.addAttribute("subTotal",subTotal);
                session.setAttribute("subTotal",subTotal);
                return "cart/Cart";
            }else {
                int subTotal = 0;
                model.addAttribute("subTotal",subTotal);
                session.setAttribute("subTotal",subTotal);
                return "cart/Cart";
            }
        }else {
            return "account/SignonForm";
        }

    }


}
